package com.ys.controller.api.hr;

import com.ys.config.MenuId;
import com.ys.entity.hr.ThrOperateCompanyBankInfo;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.hr.ThrOperateCompanyBankInfoService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 运营公司银行账户管理
 */
@Controller
@RequestMapping("/api/inter/thrOperateCompanyBankInfo")
public class ApiInterThrOperateCompanyBankInfoController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterThrOperateCompanyBankInfoController.class);

    @Autowired
    private CommonApiService<ThrOperateCompanyBankInfo> commonApiService;

    @Autowired
    private ThrOperateCompanyBankInfoService thrOperateCompanyBankInfoService;

    @RequiresPermissions("thrOperateCompanyBankInfo:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute ThrOperateCompanyBankInfo cond,String conditions){
        cond.addLeftJoin("THR_OperateCompanyApply toca ON  toca.vgsbm = A.vgsbm")
        .addLeftJoin("TS_OperateOrg C on C.Id = toca.OperateOrgId")
        .addField("C.Name operateOrgName")
        .addField("A.status statusZj")
        .addField("toca.vgsbm AS vgsbm,toca.vgsjc AS vgsjc,toca.CUser AS cUser")
        .setOrderby("A.createTime DESC");
        cond = commonApiService.dataAuth(MenuId.THR_OperateCompanyBankInfo, cond);
        return commonApiService.searchLay(cond);
    }

    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("运营公司银行账户管理新增传参：" + params);
            //修改
            return thrOperateCompanyBankInfoService.add(params);
        } catch (Exception e) {
            logger.error("运营公司银行账户管理新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }


    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("运营公司银行账户管理修改传参：" + params);
            //修改
            return thrOperateCompanyBankInfoService.edit(params);
        } catch (Exception e) {
            logger.error("运营公司银行账户管理修改出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute ThrOperateCompanyBankInfo cond, String conditions) throws Exception{

        cond.setPage(0).setSize(999999);
        cond.addLeftJoin("THR_OperateCompanyApply toca ON  toca.vgsbm = A.vgsbm")
                .addLeftJoin("TS_OperateOrg C on C.Id = toca.OperateOrgId")
                .addField("C.Name operateOrgName")
                .addField("A.status statusZj")
                .addField("toca.vgsbm AS vgsbm,toca.vgsjc AS vgsjc,toca.CUser AS cUser")
                .setOrderby("A.createTime DESC");
        cond = commonApiService.dataAuth(MenuId.THR_OperateCompanyBankInfo, cond);

        List<Map<String, Object>> listData = cond.queryMap();


        List<YsCol> listField = new YsCol()
                .setMenuId(MenuId.THR_OperateCompanyBankInfo)
                .setFrontShow(YsColEnum.FrontShow.SHOW.value)
                .setOrderby("A.SortNo asc")
                .queryList();

        ExportExcelUtils<ThrOperateCompanyBankInfo> util = new ExportExcelUtils<ThrOperateCompanyBankInfo>(new ThrOperateCompanyBankInfo());

        String toFilePath = util.exportExcel(listData, listField, "运营公司银行账户管理表");
        UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }


    @RequestMapping("/searchForModal")
    @ResponseBody
    public LayuiTableVoJA searchForModal(@ModelAttribute ThrOperateCompanyBankInfo cond,String conditions){
        cond.addLeftJoin("THR_OperateCompanyApply toca ON  toca.vgsbm = A.vgsbm")
                .addLeftJoin("TS_OperateOrg C on C.Id = toca.OperateOrgId")
                .addField("C.Name operateOrgName")
                .addField("toca.vgsjc AS vgsjc")

                .addLeftJoin("TB_StaticConfig D ON D.Lev=toca.OperationsAttribute AND D.KeyTwo = 'OperateCompanyApply-OperationsAttribute' ")
                .addField("D.NameSub operationsAttributeTxt")

                .setOrderby("A.vgsbm ASC")
                .setStatus(1);
        cond = commonApiService.dataAuth(MenuId.THR_OperateCompanyBankInfo, cond);
        return commonApiService.searchLay(cond);
    }

    @RequestMapping("/searchForModalYfksq")
    @ResponseBody
    public LayuiTableVoJA searchForModalYfksq(@ModelAttribute ThrOperateCompanyBankInfo cond,String conditions){
        cond.addLeftJoin("THR_OperateCompanyApply THRO ON  THRO.vgsbm = A.vgsbm")
                .addLeftJoin("TS_OperateOrg C on C.Id = THRO.OperateOrgId")
                .addField("C.Name operateOrgName")
                .addField("THRO.vgsjc AS vgsjc,THRO.vgsbm AS vgsbm")

                .addLeftJoin("TB_StaticConfig D ON D.Lev=THRO.OperationsAttribute AND D.KeyTwo = 'OperateCompanyApply-OperationsAttribute' ")
                .addField("D.NameSub operationsAttributeTxt")


                .setOrderby("A.vgsbm ASC")
                .setStatus(1);
        cond = commonApiService.dataAuth(MenuId.TCW_PaymentReturn, cond);
        return commonApiService.searchLay(cond);
    }

}
